package cn.the.angry.util;

import java.awt.Color;
import java.awt.Graphics2D;

import org.jbox2d.common.Color3f;
import org.jbox2d.common.IViewportTransform;
import org.jbox2d.common.OBBViewportTransform;
import org.jbox2d.common.Vec2;
import org.jbox2d.common.XForm;
import org.jbox2d.dynamics.DebugDraw;

public class AngryPigDebugDraw extends DebugDraw {
	
	private final float mScale;
	private Graphics2D mG2;
	
	
	//继承于父类的构造,是否会被使用仍然是一个谜
//	public AngryPigDebugDraw(IViewportTransform viewport) {
//		super(viewport);
//	}
	
	//
	public AngryPigDebugDraw(int width, int height, float scale){
		super(new OBBViewportTransform());
		
		mScale = scale;
		//暂定的视角(还是中心?)为长高的中点
		viewportTransform.setCamera(width/2/scale, height/2/scale, scale);
		//设定半高，半宽，必须事原始值，未被scale过，或transform过
		viewportTransform.setExtents(width/2, height/2);
		
		//设置某些奇妙的东西
		appendFlags(DebugDraw.e_shapeBit);
		appendFlags(DebugDraw.e_jointBit);
		appendFlags(DebugDraw.e_coreShapeBit);
		appendFlags(DebugDraw.e_aabbBit);
		appendFlags(DebugDraw.e_obbBit);
		appendFlags(DebugDraw.e_pairBit);
		appendFlags(DebugDraw.e_centerOfMassBit);
		appendFlags(DebugDraw.e_controllerBit);
	}
	
	//设置绘制的图形对象
	public void setGraphics(Graphics2D g){
		mG2 = g;
	}

	@Override
	public void drawPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
		System.out.println("Polygon");
		
		Color oldColor = mG2.getColor();
		mG2.setColor(new Color(color.x / 255, color.y / 255, color.z / 255));
		int i = 1;
		Vec2 v1=new Vec2(),v2=new Vec2();
		for(;i<vertexCount;++i)
			{
			viewportTransform.getWorldToScreen(vertices[i-1], v1);
			viewportTransform.getWorldToScreen(vertices[i], v2);
			mG2.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y);
			}
		viewportTransform.getWorldToScreen(vertices[i-1], v1);
		viewportTransform.getWorldToScreen(vertices[0], v2);
		mG2.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y);
	    mG2.setColor(oldColor);
	}

	@Override
	public void drawSolidPolygon(Vec2[] vertices, int vertexCount, Color3f color) {
		// TODO Auto-generated method stub
		System.out.println("SolidPolygon");
	}

	@Override
	public void drawCircle(Vec2 center, float radius, Color3f color) {
		System.out.println("Circle");
		
		Color oldColor = mG2.getColor();
		mG2.setColor(new Color(color.x / 255, color.y / 255, color.z / 255));
		Vec2 v = new Vec2(center.x - radius, center.y - radius);
	    //viewportTransform.getWorldToScreen(v, v);
	    int r = (int) (radius * mScale);
	    mG2.drawArc((int) v.x, (int) v.y, 2 * r, 2 * r, 0, 360);
	    mG2.setColor(oldColor);
	}

	@Override
	public void drawSolidCircle(Vec2 center, float radius, Vec2 axis,
			Color3f color) {
		// TODO Auto-generated method stub
		System.out.println("SolidCircle");

	}

	@Override
	public void drawPoint(Vec2 position, float f, Color3f color) {
		// TODO Auto-generated method stub
		System.out.println("Point");
		final Color oldColor=mG2.getColor();
		mG2.setColor(new Color((int) color.x / 255, (int) color.y / 255, (int) color.z / 255));
		mG2.fillRect((int) position.x, (int) position.y, 1, 1);
		mG2.setColor(oldColor);
	}

	@Override
	public void drawSegment(Vec2 p1, Vec2 p2, Color3f color) {
		// TODO Auto-generated method stub
		System.out.println("Segment");
		Color oldColor = mG2.getColor();
		mG2.setColor(new Color((int) color.x / 255, (int) color.y / 255, (int) color.z / 255));
		viewportTransform.getWorldToScreen(p1, p1);
		viewportTransform.getWorldToScreen(p2, p2);
		mG2.drawLine((int) p1.x, (int) p1.y, (int) p2.x, (int) p2.y);
	    mG2.setColor(oldColor);
	}

	@Override
	public void drawXForm(XForm xf) {
		// TODO Auto-generated method stub
		System.out.println("XForm");
	}

	@Override
	public void drawString(float x, float y, String s, Color3f color) {
		// TODO Auto-generated method stub
		System.out.println("String");
	}

}
